library(leaflet)
properties <- read.csv("properties_v2.csv")
library(dplyr)
# Categorize properties into 6 groups based on their sold price
properties1 <- filter(properties, soldprice < 1000000)
properties2 <- filter(properties, soldprice > 1000000, soldprice < 1500000)
properties3 <- filter(properties, soldprice > 1500000, soldprice < 2500000)
properties4 <- filter(properties, soldprice > 2500000, soldprice < 3500000)
properties5 <- filter(properties, soldprice > 3500000, soldprice < 5000000)
properties6 <- filter(properties, soldprice > 5000000)

# Group properties based on whether they are in the North or the South
north <- filter(properties, position == "North")
south <- filter(properties, position == "South")
library(tidyverse)
library(leaflet.extras)

# Create the labels for properties in the 1st group
info1 <- paste(
  "Address: ", properties1$address, "<br/>",
  "Distance to CBD: ", properties1$CBD_Distance, "km", "<br/>",
  "The nearest school: ", properties1$schoolName, "<br/>",
  "Distance to the nearest school: ", properties1$distanceSchool, "km", "<br/>",
  "Areas: ", properties1$landareas, "m\u00B2\n", "<br/>",
  "Sold price: $", properties1$soldprice, "<br/>",
  properties1$bedroom, " bed - ", properties1$bathroom, " bath - ", properties1$carspace, " car", "<br/>",
  "Agency: ", properties1$agency, "<br/>",
  "Sold year: ", properties1$yearsold, "<br/>",
  sep = ""
) %>% lapply(htmltools::HTML)

# Create the labels for properties in the 2nd group
info2 <- paste(
  "Address: ", properties2$address, "<br/>",
  "Distance to CBD: ", properties2$CBD_Distance, "km", "<br/>",
  "The nearest school: ", properties2$schoolName, "<br/>",
  "Distance to the nearest school: ", properties2$distanceSchool, "km", "<br/>",
  "Areas: ", properties2$landareas, "m\u00B2\n", "<br/>",
  "Sold price: $", properties2$soldprice, "<br/>",
  properties2$bedroom, " bed - ", properties2$bathroom, " bath - ", properties2$carspace, " car", "<br/>",
  "Agency: ", properties2$agency, "<br/>",
  "Sold year: ", properties2$yearsold, "<br/>",
  sep = ""
) %>% lapply(htmltools::HTML)

# Create the labels for properties in the 3rd group
info3 <- paste(
  "Address: ", properties3$address, "<br/>",
  "Distance to CBD: ", properties3$CBD_Distance, "km", "<br/>",
  "The nearest school: ", properties3$schoolName, "<br/>",
  "Distance to the nearest school: ", properties3$distanceSchool, "km", "<br/>",
  "Areas: ", properties3$landareas, "m\u00B2\n", "<br/>",
  "Sold price: ", properties3$soldprice, "<br/>",
  properties3$bedroom, " bed - ", properties3$bathroom, " bath - ", properties3$carspace, " car", "<br/>",
  "Agency: ", properties3$agency, "<br/>",
  "Sold year: ", properties3$yearsold, "<br/>",
  sep = ""
) %>% lapply(htmltools::HTML)

# Create the labels for properties in the 4th group
info4 <- paste(
  "Address: ", properties4$address, "<br/>",
  "Distance to CBD: ", properties4$CBD_Distance, "km", "<br/>",
  "The nearest school: ", properties4$schoolName, "<br/>",
  "Distance to the nearest school: ", properties4$distanceSchool, "km", "<br/>",
  "Areas: ", properties4$landareas, "m\u00B2\n", "<br/>",
  "Sold price: $", properties4$soldprice, "<br/>",
  properties4$bedroom, " bed - ", properties4$bathroom, " bath - ", properties4$carspace, " car", "<br/>",
  "Agency: ", properties4$agency, "<br/>",
  "Sold year: ", properties4$yearsold, "<br/>",
  sep = ""
) %>% lapply(htmltools::HTML)

# Create the labels for properties in the 5th group
info5 <- paste(
  "Address: ", properties5$address, "<br/>",
  "Distance to CBD: ", properties5$CBD_Distance, "km", "<br/>",
  "The nearest school: ", properties5$schoolName, "<br/>",
  "Distance to the nearest school: ", properties5$distanceSchool, "km", "<br/>",
  "Areas: ", properties5$landareas, "m\u00B2\n", "<br/>",
  "Sold price: $", properties5$soldprice, "<br/>",
  properties5$bedroom, " bed - ", properties5$bathroom, " bath - ", properties5$carspace, " car", "<br/>",
  "Agency: ", properties5$agency, "<br/>",
  "Sold year: ", properties5$yearsold, "<br/>",
  sep = ""
) %>% lapply(htmltools::HTML)

# Create the labels for properties in the 6th group
info6 <- paste(
  "Address: ", properties6$address, "<br/>",
  "Distance to CBD: ", properties6$CBD_Distance, "km", "<br/>",
  "The nearest school: ", properties6$schoolName, "<br/>",
  "Distance to the nearest school: ", properties6$distanceSchool, "km", "<br/>",
  "Areas: ", properties6$landareas, "m\u00B2\n", "<br/>",
  "Sold price: $", properties6$soldprice, "<br/>",
  properties6$bedroom, " bed - ", properties6$bathroom, " bath - ", properties6$carspace, " car", "<br/>",
  "Agency: ", properties6$agency, "<br/>",
  "Sold year: ", properties6$yearsold, "<br/>",
  sep = ""
) %>% lapply(htmltools::HTML)

# Create the choropleth map
map <- leaflet(properties) %>% 
  setView(lng = 151.2093, lat = -33.8688, zoom = 9.45) %>% 
  addTiles() %>% # rectangles separating South and North Sydney
  addRectangles(
    lng1 = max(north$longitude),
    lng2 = min(north$longitude),
    lat1 = max(north$latitute),
    lat2 = min(north$latitute),
    color = "#FFB3B3",
    fillOpacity = 0.5
  ) %>% 
  addRectangles(
    lng1 = max(south$longitude),
    lng2 = min(south$longitude),
    lat1 = max(south$latitute),
    lat2 = min(south$latitute),
    color = "#C6C5F3",
    fillOpacity = 0.5
  ) %>% # add circles to represent the sold prices on the map
  addCircles(lng = properties1$longitude,
             lat = properties1$latitute,
             radius = 20,
             color = "lightgreen",
             fillOpacity = 1,
             label = info1) %>% 
  addCircles(lng = properties2$longitude,
             lat = properties2$latitute,
             radius = 20, 
             color = "green",
             fillOpacity = 2,
             label = info2) %>% 
  addCircles(lng = properties3$longitude,
            lat = properties3$latitute,
            radius = 20,
            color = "lightyellow",
            fillOpacity = 3,
            label = info3) %>% 
  addCircles(lng = properties4$longitude,
             lat = properties4$latitute,
             radius = 20,
             color = "yellow",
             fillOpacity = 4,
             label = info4) %>% 
  addCircles(lng = properties5$longitude,
             lat = properties5$latitute,
             radius = 20, 
             color = "orange",
             fillOpacity = 2,
             label = info5) %>% 
  addCircles(lng = properties6$longitude,
             lat = properties6$latitute,
             radius = 20, 
             color = "red",
             fillOpacity = 2,
             label = info6) %>% 
  addMarkers( # marking CBD on the map
    lng = 151.2073, 
    lat = -33.8708, 
    popup = "CBD") %>% 
  addLegend(
    title = "House prices",
    position = "bottomright",
    colors = c("lightgreen", "green", "lightyellow", "yellow", "orange", "red"),
    labels = c("< $1,000,000", "$1,000,000 - $1,500,000", "$1,500,000 - $2,500,000", "$2,500,000 - $3,500,000", "$3,500,000 - $5,000,000", "> $5,000,000")
  ) %>% 
  addLegend(
    title = "Regions",
    position = "bottomleft",
    colors = c("#FFB3B3", "#C6C5F3"),
    labels = c("North Sydney", "South Sydney")
  )

map